﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XamlInspector.Foundation;

namespace XamlInspector.Model
{
    public class XamlTreeModel
    {
        private readonly MergeableObservableCollection<XamlElement> _visualTree = new MergeableObservableCollection<XamlElement>();

        public XamlTreeModel()
        {
            var frame = Window.Current.Content as Frame;
            Root = new XamlElement(frame);

            var timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Tick += (s, e) => _visualTree.Merge(Root.AllChildren.Union(new[] { Root }));
            timer.Start();

        }

        public ObservableCollection<XamlElement> AllElements
        {
            get { return _visualTree; }
        }

        public XamlElement XamlElementFromUIElement(UIElement uiElement)
        {
            return _visualTree.FirstOrDefault(x => x.Element == uiElement);
        }

        public XamlElement Root { get; private set; }

    }
}
